<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-left">
        <div class="login-left-content">
          <h1 class="system-title">小区智慧停车场管理系统</h1>
          <p class="system-desc">提供智能停车解决方案，改善小区停车体验</p>
          <div class="feature-list">
            <div class="feature-item">
              <el-icon><ElIconLocation /></el-icon>
              <span>智能车位查询</span>
            </div>
            <div class="feature-item">
              <el-icon><ElIconCalendar /></el-icon>
              <span>便捷车位预约</span>
            </div>
            <div class="feature-item">
              <el-icon><ElIconMoney /></el-icon>
              <span>在线便捷缴费</span>
            </div>
            <div class="feature-item">
              <el-icon><ElIconDataAnalysis /></el-icon>
              <span>停车数据分析</span>
            </div>
          </div>
        </div>
      </div>
      <div class="login-right">
        <div class="login-form-wrapper">
          <div class="login-welcome">
            <h2>欢迎登录</h2>
            <p>请使用您的账号密码登录系统</p>
          </div>
          <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" size="large">
            <el-form-item prop="username">
              <el-input 
                v-model="loginForm.username" 
                placeholder="用户名" 
                :prefix-icon="User"
              />
            </el-form-item>
            <el-form-item prop="password">
              <el-input 
                v-model="loginForm.password" 
                type="password" 
                placeholder="密码" 
                :prefix-icon="Lock"
                show-password
              />
            </el-form-item>
            <div class="login-options">
              <el-checkbox v-model="rememberMe">记住我</el-checkbox>
              <el-link type="primary" :underline="false">忘记密码?</el-link>
            </div>
            <el-form-item>
              <el-button 
                type="primary" 
                :loading="loading" 
                @click="handleLogin" 
                class="login-button"
              >登录</el-button>
            </el-form-item>
          </el-form>
          <div class="login-footer">
            <span>还没有账号?</span>
            <el-link type="primary" @click="goRegister">立即注册</el-link>
          </div>
          
          <div class="login-quick-access">
            <p>测试账号快捷登录:</p>
            <div class="quick-buttons">
              <el-button @click="quickLogin('admin', '123456')">管理员账号</el-button>
              <el-button @click="quickLogin('user', '123456')">普通用户账号</el-button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, Location, Calendar, Money, DataAnalysis } from '@element-plus/icons-vue'
import { useUserStore } from '@/store/user'

// 图标组件
const ElIconLocation = Location
const ElIconCalendar = Calendar
const ElIconMoney = Money
const ElIconDataAnalysis = DataAnalysis

// 路由实例
const router = useRouter()
// 用户状态
const userStore = useUserStore()

// 登录表单ref
const loginFormRef = ref(null)
// 登录加载状态
const loading = ref(false)
// 记住我选项
const rememberMe = ref(false)

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: ''
})

// 登录表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 2, max: 20, message: '用户名长度应为2-20个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应为6-20个字符', trigger: 'blur' }
  ]
}

// 处理登录
const handleLogin = async () => {
  // 表单验证
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        // 调用登录接口
        await userStore.loginAction(loginForm)
        
        ElMessage.success('登录成功')
        
        // 根据用户角色跳转到相应的首页
        if (userStore.isAdmin) {
          router.push('/admin/home')
        } else {
          router.push('/user/home')
        }
      } catch (error) {
        ElMessage.error(error.message || '登录失败')
      } finally {
        loading.value = false
      }
    }
  })
}

// 快捷登录
const quickLogin = (username, password) => {
  loginForm.username = username
  loginForm.password = password
  handleLogin()
}

// 跳转到注册页面
const goRegister = () => {
  router.push('/register')
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.login-box {
  display: flex;
  width: 900px;
  height: 600px;
  border-radius: 15px;
  overflow: hidden;
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
}

.login-left {
  flex: 1;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px;
}

.login-left-content {
  text-align: center;
}

.system-title {
  font-size: 28px;
  margin-bottom: 10px;
  font-weight: bold;
}

.system-desc {
  font-size: 16px;
  margin-bottom: 40px;
  opacity: 0.9;
}

.feature-list {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
  margin-top: 40px;
}

.feature-item {
  display: flex;
  align-items: center;
  background: rgba(255, 255, 255, 0.15);
  padding: 15px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.feature-item:hover {
  background: rgba(255, 255, 255, 0.25);
  transform: translateY(-3px);
}

.feature-item i {
  font-size: 22px;
  margin-right: 10px;
}

.login-right {
  flex: 1;
  background: white;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-form-wrapper {
  width: 80%;
  max-width: 350px;
}

.login-welcome {
  text-align: center;
  margin-bottom: 30px;
}

.login-welcome h2 {
  font-size: 24px;
  margin-bottom: 8px;
  color: #333;
}

.login-welcome p {
  color: #666;
  font-size: 14px;
}

.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.login-button {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
}

.login-footer {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
  color: #666;
}

.login-quick-access {
  margin-top: 30px;
  text-align: center;
  padding-top: 20px;
  border-top: 1px dashed #ddd;
}

.login-quick-access p {
  margin-bottom: 10px;
  color: #666;
  font-size: 14px;
}

.quick-buttons {
  display: flex;
  justify-content: space-around;
}

/* 移动端适配 */
@media screen and (max-width: 768px) {
  .login-box {
    width: 90%;
    flex-direction: column;
    height: auto;
  }
  
  .login-left {
    padding: 30px 20px;
  }
  
  .login-right {
    padding: 30px 0;
  }
  
  .feature-list {
    grid-template-columns: 1fr;
  }
}
</style> 